import inspect
from common.KoufuConf import KoufuConf

__author__ = 'yangxianming'



class KoufuShopApi:
    __koufu_conf = KoufuConf()

    @staticmethod
    def __update_body(body, update_body="", del_body_key=""):
        #更新或删除接口数据
        if update_body != "":
            body.update(update_body)
        if del_body_key != "":
            if "," in del_body_key:
                for one_key in del_body_key.split(","):
                    del body[one_key]
            else:
                del body[del_body_key]
        return body

    @staticmethod
    def __post_body_to_get_body(body):
        get_str = ""
        for (key, value) in body.items():
            get_str = get_str + "&" + key + "=" + str(value)
        get_str = get_str[1:]
        return get_str

    @staticmethod
    def __get_form_data(body, boundary):
        #将数据转换为 form_data 的body格式
        form_data = ""
        for (key, value) in body.items():
            form_data = form_data + "--" + boundary + "\r\n"
            form_data = form_data + "Content-Disposition: form-data; name=\"" + key + "\"\r\n"
            form_data += "Content-Type: text/plain; charset=UTF-8\r\n"
            form_data += "Content-Transfer-Encoding: 8bit\r\n\r\n"
            form_data = form_data + value + "\r\n"
        form_data = form_data + "--" + boundary + "--\r\n\r\n\r\n"
        # print("|" + form_data + "|")
        return form_data

    #update_body:{}  del_body_key:key值 使用,分割多个值


     #口福商城后管 商品列表 get
    def get_catagory_all_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/catagory/all"
        default_body = {"parentId":0,"request_id":"123456"}
        if get_path == 1:
            return path
        return self.__update_body(self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key))

    #口福商城后管 添加商品类星
    def get_category_save_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/category/save"
        default_body = {"isShow":1,"type":0,"level":"L2","bannerUrl":"","iconUrl":"","imgUrl":"","wapBannerUrl":"","name":"电火锅","frontDesc":"电火锅","parentId":1036047,"showIndex":"1"}
        if get_path == 1:
            return path
        return self.__update_body(self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key))

    #口福商城后管 商品列表 get
    def get_goods_list_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/goods/list"
        default_body = {"_search":"false","nd":"1563940342134","limit":"100","page":"1","sidx":"","order":"asc","_":"1563940341629"}
        if get_path == 1:
            return path
        return self.__post_body_to_get_body(self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key))

    #商城2.0 券详情查询
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&apiID=657&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_order_voucherDetail_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/order/voucherDetail"
        default_body = {"voucherId":0,"requestId":"123456"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城2.0 订单详情查询
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&childGroupID=93&apiID=656&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_order_orderDetail_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/order/orderDetail"
        default_body = {"orderSn":"", "requestId":"123456"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城2.0 支付宝调起支付接口
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&childGroupID=93&apiID=651&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_pay_ali_pay_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/pay/smartPay"
        default_body = {"orderSn":"", "requestId":"123456","payId":"00"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城 支付方式获取
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=-1&apiID=679&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_pay_payment_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/pay/payment"
        default_body = {"requestId":"123456"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城 聚合支付
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=-1&apiID=681&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_pay_smartPay_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/pay/smartPay"
        default_body = {"requestId":1634886653137,"orderSn":"","payId":"02"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城2.0 直接购买(无购物车)
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&childGroupID=93&apiID=650&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_order_atOnceSubmit_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/order/atOnceSubmit"
        default_body = {"requestId":1634885119234,"productId":"","activityId":"","addressId":56,"number":1,"realName":"","certNo":"","exchange":""}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城2.0 口福授权接口
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&apiID=649&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_auth_koufu_authorize_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/auth/koufu_authorize"
        default_body = {"name":"yangxianming","channelId":"koufu1234567890","thirdUserId":"040351271326585810","corpId":self.__koufu_conf.corpId}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #/api/exchange/goods/seckill
    #秒杀活动商品列表
    def get_api_exchange_goods_seckill_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/exchange/goods/seckill"
        default_body = {"requestId":1577439350084}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #/api/goods/product/list
    #秒杀活动商品列表
    def get_api_goods_product_list(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/goods/product/list"
        default_body = {"request_id":1577439350084,"corpId":"dingf686af244fa39dcf35c2f4657eb6378f"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #/api/goods/es/good/keywords
    #商品全文搜索关键字接口    "corpId":"dingf686af244fa39dcf35c2f4657eb6378f"
    def get_api_goods_es_good_keywords(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/goods/es/good/keywords"
        default_body = {"request_id":1604312892119,"page":1,"size":200,}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #/api/goods/es/product/list
    #商品全文搜索关键字接口    "corpId":"dingf686af244fa39dcf35c2f4657eb6378f"
    def get_api_goods_es_product_list(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/goods/es/product/list"
        default_body = {"request_id":1604312892119,"page":1,"size":200,"saleTotalNum":"","descPrice":"","ascPrice":""}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)


    #商城2.0 支付支付回调mock接口 （废弃，开发内部删除了mock接口）
    def get_api_pay_alipay_notify_mock_data(self, get_path=0, boundary=__koufu_conf.boundary, update_body="", del_body_key=""):
        path = "/koufu-shop/api/pay/alipay/notify/mock"
        default_body = {"out_trade_no":"", "gmt_payment":"2019-07-15 11:15:00"}
        if get_path == 1:
            return path
        body = self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)
        return self.__get_form_data(body, boundary)

    def get_funtion_name(self):
        api_list = []
        for one in dir(self):
            if "_data" in one and "get_" in one:
                api_list.append(one[4:-5].replace("_", "."))
        return api_list
